package com.itheima.admin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Spring Security配置
 * 所有请求都允许通过，由网关统一鉴权
 * 
 * @author itheima
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 密码加密器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 禁用CSRF，因为使用JWT
        http.csrf().disable()
                // 禁用表单登录
                .formLogin().disable()
                // 禁用HTTP基本认证
                .httpBasic().disable()
                // 所有请求都允许通过（由网关统一鉴权）
                .authorizeRequests()
                .anyRequest().permitAll();
    }
}

